﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace KhatVongViet.Models
{
    public class GetData
    {
        KhatVongVietEntities dataContext = new KhatVongVietEntities();

        /// <summary>
        /// Gets an article by ID.
        /// </summary>
        /// <param name="idArticle">The ID of article.</param>
        /// <returns>The Article by ID.</returns>
        public Article GetArticle(int idArticle)
        {
            Article article = (from p in dataContext.Articles select p)
                .FirstOrDefault(p => p.ID == idArticle);
            return article;
        }

        //Lấy các bài viết cũ hơn bài viết hiện tại.
        public List<Article> GetListOldArticle(int idArticle)
        {
            var article = (from p in dataContext.Articles select p)
                .FirstOrDefault(p => p.ID == idArticle);

            var listArticle = new List<Article>();

            if (article != null && article.DateCreate != null)
            {
                listArticle = ((from p in dataContext.Articles
                                where ((DateTime)article.DateCreate > (DateTime)p.DateCreate && p.TypeID != "GocChiaSe_CXV" && p.ID != article.ID)
                                select p)
                .OrderByDescending(p => p.DateCreate).Take(10)).ToList();
            }

            return listArticle;
        }

        //Lấy các bài viết mới hơn bài viết hiện tại.
        public List<Article> GetListNewArticle(int idArticle)
        {
            var article = (from p in dataContext.Articles select p)
                .FirstOrDefault(p => p.ID == idArticle);

            var listArticle = new List<Article>();

            if (article != null && article.DateCreate != null)
            {
                listArticle = ((from p in dataContext.Articles
                                where ((DateTime)article.DateCreate < (DateTime)p.DateCreate && p.TypeID != "GocChiaSe_CXV" && p.ID != article.ID)
                                select p)
                .OrderByDescending(p => p.DateCreate).Take(10)).ToList();
            }

            return listArticle;
        }

        /// <summary>
        /// Gets list articles in ArticleDetail page (Image).
        /// </summary>
        /// <param name="idArticle"></param>
        /// <returns></returns>
        public List<Article> GetListOtherArticle(int idArticle)
        {
            var article = GetArticle(idArticle);

            var listArticle = ((from p in dataContext.Articles
                                where (p.TypeID != "GocChiaSe_CXV" && p.ID != idArticle && p.TypeID == article.TypeID)
                                select p)
                .OrderByDescending(p => p.DateCreate).Take(3)).ToList();

            return listArticle;
        }

        /// <summary>
        ///     Gets the list of new articles.
        /// </summary>
        /// <returns>The list of new articles.</returns>
        public List<Article> GetNewsArticle()
        {
            var listArticle = ((from p in dataContext.Articles
                                where p.TypeID.Contains("TinTuc")
                                select p)
                .OrderByDescending(p => p.DateCreate).Take(5)).ToList();
            return listArticle;
        }

        /// <summary>
        ///     Gets the list of new CamXuc.
        /// </summary>
        /// <returns>The list of new CamXuc.</returns>
        public List<Article> GetTopCamXuc()
        {
            List<Article> listArticle = ((from p in dataContext.Articles
                                          where p.Publish == true && p.TypeID == "GocChiaSe_CXV"
                                          select p)
                .OrderByDescending(p => p.DateCreate).Take(6)).ToList();

            return listArticle;
        }

        public List<Article> GetActivitiesArticles()
        {
            var listArticle = (from p in dataContext.Articles where p.TypeID.Contains("HoatDong") select p)
                .OrderByDescending(p => p.DateCreate).Take(4).ToList();
            return listArticle;
        }

        /// <summary>
        ///     Gets all Articles.
        /// </summary>
        /// <returns>The list of articles.</returns>
        public List<Article> GetAllArticle()
        {
            return (from p in dataContext.Articles orderby p.ID ascending select p).ToList();
        }

        public List<Article> GetAllDetail()
        {
            return (from p in dataContext.Articles where (p.TypeID.Contains("HoatDong") || p.TypeID.Contains("TinTuc")) select p)
                .OrderByDescending(p => p.DateCreate).ToList();
        }

        public List<Article> GetAllDetailNews()
        {
            return (from p in dataContext.Articles where p.TypeID.Contains("TinTuc") select p)
                .OrderByDescending(p => p.DateCreate).ToList();
        }

        public List<Article> GetAllDetailActivities()
        {
            return (from p in dataContext.Articles where p.TypeID.Contains("HoatDong") select p)
                .OrderByDescending(p => p.DateCreate).ToList();
        }

        //Lấy các bài báo hoạt động đã diễn ra
        public List<Article> GetAllDetailActivitiesPast()
        {
            return
                (from p in dataContext.Articles where (p.TypeID.Contains("HoatDong") && p.DateCreate < DateTime.Now) select p)
                    .OrderByDescending(p => p.DateCreate).ToList();
        }

        //Lấy các bài báo hoạt động sắp diễn ra
        public List<Article> GetAllDetailActivitiesFuture()
        {
            return
                (from p in dataContext.Articles where (p.TypeID.Contains("HoatDong") && p.DateCreate > DateTime.Now) select p)
                    .OrderByDescending(p => p.DateCreate).ToList();
        }

        public List<Article> GetAllDetailConfession()
        {
            return (from p in dataContext.Articles where (p.TypeID == "GocChiaSe_CXV") select p)
                .OrderByDescending(p => p.DateCreate).ToList();
        }

        //Lấy dữ liệu cho mục thông báo trên menu
        public List<Article> GetAllDetailNotice()
        {
            return (from p in dataContext.Articles where p.TypeID.Contains("ThongBao") select p)
                .OrderByDescending(p => p.DateCreate).ToList();
        }

        /// <summary>
        /// Get Cam Cuc Article.
        /// </summary>
        /// <param name="id">The Id of Cam Xuc Article.</param>
        /// <returns>The Cam Xuc Article.</returns>
        public Article GetCamXucViet(int id)
        {
            Article article = (from a in dataContext.Articles where a.ID == id select a).FirstOrDefault();

            return article;
        }

        /// <summary>
        ///     Get User based on Id.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>The User.</returns>
        public User GetUser(string username, string password)
        {
            User user = (from p in dataContext.Users select p).FirstOrDefault(p => p.UserName == username && p.Password == password);

            return user;
        }

        //lay du lieu phan tin tuc 
        public List<Article> GetTopNewNews()
        {
            var listArticle = (from p in dataContext.Articles where p.TypeID.Contains("TinTuc") select p)
                .OrderByDescending(p => p.DateCreate).Take(6).ToList();
            return listArticle;
        }

        public List<Article> GetNoticesArticles()
        {
            return (from p in dataContext.Articles where p.TypeID.Contains("ThongBao") select p)
                .OrderByDescending(p => p.DateCreate).Take(4).ToList();
        }

        public List<Article> GetListSlideShow()
        {
            return (from p in dataContext.Articles where p.TypeID == "SlideShow" select p)
                .OrderByDescending(p => p.DateCreate).Take(3).ToList();
        }

        //Get list member with RoleId equals id
        public List<Member> GetListMembers(string id)
        {
            return (from p in dataContext.Members where p.RoleID == id select p).ToList();
        }

        public Member GetMember(int id)
        {
            Member member = (from a in dataContext.Members where a.ID == id select a).FirstOrDefault();

            return member;
        }

        /// <summary>
        /// Gets 4 articles that has the most viewer.
        /// </summary>
        /// <returns></returns>
        public List<Article> GetMostViewer()
        {
            List<Article> listArticle = (from p in dataContext.Articles where p.TypeID.Contains("TinTuc") select p)
                .OrderByDescending(p => p.ViewNumber).Take(4).ToList();
            return listArticle;
        }

        /// <summary>
        /// Gets 4 hot news.
        /// </summary>
        /// <returns></returns>
        public List<Article> GetHotNews()
        {
            var listArticles = new List<Article>();

            //Gets a News article.
            Article newsArticle =
                (from p in dataContext.Articles where p.TypeID.Contains("TinTuc") select p).OrderByDescending(p => p.DateCreate)
                    .FirstOrDefault();
            if (newsArticle != null)
            {
                listArticles.Add(newsArticle);
            }

            //Gets a Activity article.
            List<Article> activityArticle =
                (from p in dataContext.Articles where p.TypeID.Contains("HoatDong") select p).OrderByDescending(p => p.DateCreate)
                    .Take(2).ToList();
            if (activityArticle.Any())
            {
                listArticles.AddRange(activityArticle);
            }

            //Gets a Notice article.
            Article noticeArticle =
                (from p in dataContext.Articles where p.TypeID == "ThongBao" select p).OrderByDescending(p => p.DateCreate)
                    .FirstOrDefault();
            if (noticeArticle != null)
            {
                listArticles.Add(noticeArticle);
            }

            //Gets a Notice article.
            Article libraryArticle =
                (from p in dataContext.Articles where p.TypeID.Contains("GocChiaSe") select p).OrderByDescending(p => p.DateCreate)
                    .FirstOrDefault();
            if (libraryArticle != null)
            {
                listArticles.Add(libraryArticle);
            }

            return listArticles;
        }
    }
}